package Shop;

import MannageService.Manage;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/shows-hop")
public class showservlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private ShopDaoImp ShopDao;
    private int page;
    private int allpage;
    public void init() {
        ShopDao = new ShopDaoImp();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, IOException {


        int page = 1;
        if (request.getParameter("page") != null) {
            page = Integer.parseInt(request.getParameter("page"));
            System.out.println("当前页数为:"+page);
            System.out.println("page="+page);
        }
        HttpSession session = request.getSession(true);
        //Shop shop= (Shop) session.getAttribute("manage");
        Manage manage= (Manage) session.getAttribute("manage");
        allpage= ShopDao.count(manage.getMname())/4;
        System.out.println(allpage);
        if (ShopDao.count(manage.getMname())%4==0){
            allpage=allpage;
        }else {
            allpage=allpage+1;
        }
        if (page>=allpage){
                page=allpage;
            System.out.println("大于总数的:"+page);
        }else if (page<=1){
            System.out.println("页数小于1的:"+page);
            page=1;
        }

        List<Shop> shops = ShopDao.pageshop(page,manage.getMname());
        request.setAttribute("shops", shops);
        request.setAttribute("page",page);
        RequestDispatcher dispatcher = request.getRequestDispatcher("showshop.jsp");
        dispatcher.forward(request, response);

    }
}